<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            上岗离岗
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">生产日期</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择日期" class="aui-input" id="prodDate">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">班&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp次</div>
                    <div class="aui-list-item-input">
                        <select id="shiftCode" onchange="searchTakePostData()">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">班&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp组</div>
                    <div class="aui-list-item-input">
                        <select id="teamCode" onchange="searchTakePostData()">
                        </select>
                    </div>
                </div>
            </li>

        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <!-- onclick="takeQuittPostClick(1)">上岗 -->
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%; background-color: green"
            onclick="doTaskUp(1)">上岗
        </div>
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%;background-color: red"
            onclick="doTaskUp(2)">离岗
        </div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'>序号</td>
                        <td algin='center'>姓名</td>
                        <!-- <td algin='center'>工号</td> -->
                        <td algin='center'>工序</td>
                        <!-- <td algin='center'>生产日期</td>
                        <td algin='center'>班次</td>
                        <td algin='center'>班组</td> -->
                        <td algin='center'>上岗时间</td>
                        <td algin='center'>离岗时间</td>
                    </tr>
                </thead>
                <tbody id="takeQuitPostDataHtml">
                </tbody>
            </table>
        </div>
    </div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var equipUniqueId = "";

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');

        //1:初始化当前日期
        crateDate();

        //2:查询班组班次下拉框
        doSearchTeamShift();

        //监听返回键
        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            if (ret) {
                closeWin();
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //获取当前日期
    function crateDate() {
        if (api.pageParam.proddate == null || api.pageParam.proddate == "" || api.pageParam.proddate == undefined) {
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            $("#prodDate").val(year + "-" + month + "-" + day);
        } else {
            $("#prodDate").val(api.pageParam.proddate);
        }
    }
    function doTaskUp(flag) {

        var proddate = $("#prodDate").val();
        var teamCode = $("#teamCode").val();
        var shiftCode = $("#shiftCode").val();
        if (proddate == null || proddate == "" || proddate == undefined) {
            api.toast({
                msg: "请选择生产日期!",
                duration: 3000
            });
            return;
        }

        if (teamCode == null || teamCode == "" || teamCode == undefined) {
            api.toast({
                msg: "请选择班组!",
                duration: 3000
            });
            return;
        }

        if (shiftCode == null || shiftCode == "" || shiftCode == undefined) {
            api.toast({
                msg: "请选择班次!",
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '识别中...',
        });

        faceDetect({ debug: 0 }, (ret) => {
            if (ret.status) {
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/loginface";

                var data = {
                    values: {
                        faceBase64: ret.face,
                        padCode:localStorage.getItem('equipUniqueId'),
                    }
                };
                api.ajax({
                    url: url,
                    method: 'post',
                    dataType: 'application/x-www-form-urlencoded',
                    timeout: 30,
                    returnAll: false,
                    data: data
                }, function (ret, err) {
                    if (ret) {

                        var map = JSON.parse(ret);

                        if (map.code == 0) {
                            doTaskUpLater(map.user, flag)
                            api.hideProgress();
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: map.msg,
                                duration: 2000
                            });
                        }
                    }
                });
            } else {
                api.hideProgress();
            }
        });
    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#prodDate").val(date);
            }
        });
    }

    //班组下拉框
    function doSearchTeamShift() {
        console.log(api.pageParam.workCenterCode);
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTeam";
        var data = {
            values: {
                workCenterCode: api.pageParam.workCenterCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {

                    //班組賦值
                    var list = eval(myobj.list);
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("teamCode"), html);
                    $("#teamCode").val(api.pageParam.teamCode);

                    //班次赋值
                    var listShift = eval(myobj.listShift);
                    var htmlShift = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < listShift.length; i++) {
                        htmlShift += "<option value='" + listShift[i].code + "'>" + listShift[i].name + "</option>";
                    }
                    $api.html($api.byId("shiftCode"), htmlShift);
                    $("#shiftCode").val(api.pageParam.shiftCode);

                    //传输参数日期班组班次不为空，查询当前上岗数据
                    if (api.pageParam.shiftCode != null && api.pageParam.shiftCode != "" && api.pageParam.shiftCode != undefined) {
                        searchTakePostData();
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询上岗信息
    function searchTakePostData() {
        var proddate = $("#prodDate").val();
        var teamCode = $("#teamCode").val();
        var shiftCode = $("#shiftCode").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTakePostData";
        var data = {
            values: {
                workCenterCode: api.pageParam.workCenterCode,
                opCode: api.pageParam.opCode,
                proddate: proddate,
                teamCode: teamCode,
                shiftCode: shiftCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {

                    //班組賦值
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                        html += "<td name='index'>" + (i + 1) + "</td>";
                        html += "<td name='empName'>" + list[i].empName + "</td>";
                        // html += "<td name='empNo'>" + list[i].empNo + "</td>";
                        html += "<td name='opCode'>" + list[i].opCode + "</td>";
                        // html += "<td name='proddate'>" + list[i].proddate + "</td>";
                        // html += "<td name='shiftName'>" + list[i].shiftName + "</td>";
                        // html += "<td name='teamName'>" + list[i].teamName + "</td>";
                        html += "<td name='startWorkTime'>" + list[i].startWorkTime + "</td>";
                        if (list[i].endWorkTime == null || list[i].endWorkTime == undefined) {
                            html += "<td name='endWorkTime'></td>";
                        } else {
                            html += "<td name='endWorkTime'>" + list[i].endWorkTime + "</td>";
                        }
                        html += "</tr>";
                    }

                    $api.html($api.byId("takeQuitPostDataHtml"), html);
                } else {
                    $api.html($api.byId("takeQuitPostDataHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    function doTaskUpLater(userCode, flag) {

        var proddate = $("#prodDate").val();
        var teamCode = $("#teamCode").val();
        var shiftCode = $("#shiftCode").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTakeQuitPost";
        var data = {
            values: {
                proddate: proddate,
                workCenterCode: api.pageParam.workCenterCode,
                workCellCode: api.pageParam.workCellCode,
                opCode: api.pageParam.opCode,
                teamCode: teamCode,
                shiftCode: shiftCode,
                userCode: userCode,
                flag: flag,
                un: un,
                isCheckPersonnelSkill:api.pageParam.isCheckPersonnelSkill,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //上岗离岗记录信息
                    searchTakePostData();
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });

    }

    //上岗/离岗
    function takeQuittPostClick(flag) {
        var proddate = $("#prodDate").val();
        var teamCode = $("#teamCode").val();
        var shiftCode = $("#shiftCode").val();
        if (proddate == null || proddate == "" || proddate == undefined) {
            api.toast({
                msg: "请选择生产日期!",
                duration: 3000
            });
            return;
        }

        if (teamCode == null || teamCode == "" || teamCode == undefined) {
            api.toast({
                msg: "请选择班组!",
                duration: 3000
            });
            return;
        }

        if (shiftCode == null || shiftCode == "" || shiftCode == undefined) {
            api.toast({
                msg: "请选择班次!",
                duration: 3000
            });
            return;
        }

        //上岗标识：0刷卡，1勾选
        var sgFlag = api.pageParam.sgFlag;

        // //刷卡
        if ("0" == sgFlag) {

            swipeCard(flag);

            //勾选
        } else {
            //     var pageParam = api.pageParam;
            //     pageParam.flag = flag;
            //     pageParam.proddate = proddate;
            //     pageParam.shiftCode = shiftCode;
            //     pageParam.teamCode = teamCode;
            //     api.openWin({
            //         name: "productionSglg_select",
            //         url: 'productionSglg_select.html',
            //         pageParam: pageParam,
            //     });
        }
    }

    //刷卡
    function swipeCard(flag) {
        var title = "";
        if (flag == 1) {
            title = "上岗";
        } else if (flag == 2) {
            title = "离岗";
        }
        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: title,
                placeholder: '扫描/录入卡号',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 240,
                title: {
                    h: 60,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 20,
                    textColor: '#000',
                    corner: 5,                  //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；左边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；左边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；左边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；左边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function (ret) {
            //工号
            var empNo = ret.text;

            //取消
            if (ret.eventType == 'left') {
                dialogBox.close({
                    dialogName: 'input'
                });

                //确定
            } else {

                //卡号必输校验
                if (empNo == null || empNo == "" || empNo == undefined) {
                    api.toast({
                        msg: "请扫描/录入卡号!",
                        duration: 3000
                    });
                } else {
                    dialogBox.close({
                        dialogName: 'input'
                    });

                    //上岗离岗保存
                    save(flag, empNo);
                }
            }
        });
    }

    //上岗/离岗保存
    function save(flag, empNo) {
        var proddate = $("#prodDate").val();
        var teamCode = $("#teamCode").val();
        var shiftCode = $("#shiftCode").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTakeQuitPostZSSave";
        var data = {
            values: {
                proddate: proddate,
                workCenterCode: api.pageParam.workCenterCode,
                workCellCode: api.pageParam.workCellCode,
                opCode: api.pageParam.opCode,
                teamCode: teamCode,
                shiftCode: shiftCode,
                flag: flag,
                empNo: empNo,
                sgFlag: api.pageParam.sgFlag,//上岗标识：0刷卡，1勾选
                un: un
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //上岗离岗记录信息
                    searchTakePostData();
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    //刷新
    function refresh() {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //上岗离岗记录信息
        searchTakePostData();
    }

    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }

    function faceDetect({ debug = 0, cropType = 3, cropHeight = 300, quality = 70 }, callback) {
        const module = api.require('dwzBaiduFaceLive');
        module.faceDetect(
            {
                debug: debug, // 调试开关(默认:0)：0, 1
                cropType: cropType, // 抠图类型(默认:1)：1:脸部, 2:大头照, 3:头像+肩膀
                cropHeight: cropHeight, // 抠图高的设定，为了保证好的抠图效果，要求高宽比是4:3，所以会在内部进行计算，只需要传入高即可，取值范围50 ~ 1200，默认480
                quality: quality // 抠图压缩质量，取值范围 20 ~ 100，默认100不压缩
            },
            (ret, err) => {
                if (!ret.status && ret.message) {
                    $.alert.toast(ret.message);
                }

                callback && callback(ret);
            }
        );
    }
</script>

</html>